import { getBaseUrl } from '@/services/urlconst';
import {
  getCompanyID,
  getPosIDs,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 添加
 * @param addClinetCallllback 新增接口调用后的回调
 * @returns
 */
const addClinetService = (addClinetCallllback: (res: any) => void) => {
  const addClinet = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        // applyerID: getUserID(),
        // applyPositionID: getPosIDs(),
        // applyDepartmentID: getUserDepID(),
        // applyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/clientController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: addClinetCallllback,
    },
  );
  return {
    addClinetRun: addClinet?.run,
    addClinetLoading: addClinet?.loading,
  };
};

/**
 * 编辑
 * @param editClientCallllback 接口调用后的回调
 * @returns
 */
const editClientService = (editClientCallllback: (res: any) => void) => {
  const editClient = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/clientController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: editClientCallllback,
    },
  );
  return {
    editClientRun: editClient?.run,
    editClientLoading: editClient?.loading,
  };
};

/**
 * 获取详情
 * @param getClientByIDCall  获取详情回调
 * @returns
 */
const getClientByIDService = (getClientByIDCall: (res: any) => void) => {
  const getClientByID = useRequest(
    (paramsId: any) => {
      let personUrl = `${baseUrl}/clientController/getClientByID/${paramsId}`;
      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: getClientByIDCall,
    },
  );

  return {
    getClientByIDRun: getClientByID?.run,
    getClientByIDLoading: getClientByID?.loading,
    getClientByIDData: getClientByID?.data?.obj,
  };
};

/**
 * 修改秘钥
 * @param modifyPwdCallllback 接口调用后的回调
 * @returns
 */
const modifyPwdService = (modifyPwdCallllback: (res: any) => void) => {
  const modifyPwd = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/clientController/modifyPwd`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modifyPwdCallllback,
    },
  );
  return {
    modifyPwdRun: modifyPwd?.run,
    modifyPwdLoading: modifyPwd?.loading,
  };
};

/**
 * 修改状态
 * @param isOpenCallllback 接口调用后的回调
 * @returns
 */
const isOpenService = (isOpenCallllback: (res: any) => void) => {
  const isOpen = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/clientController/isOpen`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: isOpenCallllback,
    },
  );
  return {
    isOpenRun: isOpen?.run,
    isOpenLoading: isOpen?.loading,
  };
};

export {
  editClientService,
  addClinetService,
  getClientByIDService,
  modifyPwdService,
  isOpenService,
};
